import msg from "./msg";
import { login, updateUserInfo } from "./request/api";
interface UserInfo {
	name: string,
	code: string,
	class_id: number,
	className: string,
	openid:string,
}
interface userAtt {
	userId: number,
	name: string,
	code: string,
	className: string,//班级名称
	class_id: number,
	openid: string,
	grade: string,
	duty_course:string[];
	s_id:number,//学生id
	isAdmin:boolean,//是否为管理员账号
	//管理员属性
	admin_name:string;
	admin_account:string;
	roleCode:string;
	roleName:string;
	admin_id:number;
}


//在user类中新增属性需要再3处增加 1.userAtt接口 2.user.setAtt()方法 3.user.dataInit()方法
export default class User {
	userId = 0 as number;
	name = '' as string;
	code = '' as string;
	grade = '' as string;
	className = '' as string;//班级名称
	class_id = 0 as number;
	openid = '' as string;
	roleCode='' as string;
	roleName='' as string;
	duty_course=[] as string[];//课代表负责的课程id
	s_id=0 as number;
	//管理员属性
	isAdmin=false as boolean;
	admin_name='' as string;
	admin_account='' as string;
	admin_id=0 as number;
	constructor() {
		//if (!this.cheakLogin()) return;
		this.dataInit();
		//console.log('user=',this);
	};
	public async Login(code: string) {
		let data:any=[];
		return new Promise(async (resolve, reject) => {
			await login({
				code: code
			}).then((res: any) => {
				if (!res.data.code) return;
				data = res.data;
				//console.log('data=',res.data.data);
				this.setAtt(res.data.data);
				this.setStorage();
			});
			resolve(data);
		});
	};
	public setAtt(data: userAtt | User) {
		if(data.userId){this.userId = data.userId;}
		if(data.openid){this.openid = data.openid;}
		if(data.name)this.name = data.name;
		if(data.code)this.code = data.code;
		if(data.className)
		this.className = data.className;
		if(data.grade)
		this.grade = data.grade;
		if(data.class_id)
		this.class_id=data.class_id;
		if(data.duty_course&&data.duty_course[0]!="")
		this.duty_course=data.duty_course;
		if(data.isAdmin)
		this.isAdmin=true;
		//管理员属性
		if(data.admin_name)
		this.admin_name=data.admin_name;
		if(data.admin_account)
		this.admin_account=data.admin_account;
		if(data.admin_id)
		this.admin_id=data.admin_id;
		if(data.roleCode)
		this.roleCode=data.roleCode;
		else this.roleCode='0';
		if(data.roleName)
		this.roleName=data.roleName;
		else this.roleName='普通学生';
	}
	//检查是否可以签到，报名，即基本信息是否完善
	private cheakValid(): boolean {
		if (!this.cheakLogin()) return false;
		if (this.name && this.code && this.class_id) return true;
		return false;
	};
	//设置全局变量
	public setStorage() {
		wx.setStorageSync('user', this);
		//console.log('setUser=',this);
	};
	//拿取用户可以填写的数据
	public getInfo() {
		return {
			name: this.name,
			code: this.code,
			class_id: this.class_id,
			className:  this.className,
			openid:this.openid,
		};
	};
	//学生用户绑定信息
	public async updateInfo(param: UserInfo) {
		this.code = param.code;
		this.class_id = param.class_id;
		this.openid = param.openid;
		return new Promise(async (resolve, reject) => {
			await updateUserInfo({ code: this.code, class_id: this.class_id, userId: this.userId ,openid:this.openid})
				.then((res: any) => {
					if (res.data.code == '200'){
						this.setAtt(res.data.data);
						this.setStorage();
						msg.show('绑定学生信息成功', '');
					}
					else if(res.data.code=='201'){
						msg.show('该微信号已经绑定了学号为:'+res.data.data+'的学生','如要更换绑定学生请联系管理员');
					}
					else msg.show('绑定学生信息失败', '请检查姓名,学号,班级是否有误');
					resolve(res.data.code);
				});
		}).catch((e:any) => {});
	};
	//检查是否已登录
	public cheakLogin(param: number = 0): boolean {
		//console.log('user=',user_temp);
		//未登录
		if (this.userId == undefined || this.userId == 0 ) {
			if (param != 0) {
				msg.confirm('是否进行登陆/注册', '')
					.then(() => {
						wx.navigateTo({
							url: "/pages/user/login/login",
						})
					})
			}
			return false;
		}
		//已经登陆
		else {
			return true;
		}
	};
	//登出
	public logOut(): void {
		console.log('logout!');
		wx.removeStorageSync('user');
		
	};
	public dataInit(): void {
		let user_temp: User = wx.getStorageSync('user');
		//console.log('datainit=',user_temp);
		this.setAtt(user_temp);
	};
}
export {
	UserInfo,
}